package com.zh.analysis.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zh.analysis.service.XiaoMiService;
import com.zh.device.service.RedisDeviceService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 环境监测设备
 */
public class XiaoMiServiceImpl implements XiaoMiService {

    @Autowired
    private RedisDeviceService redisDeviceService;

    @Override
    public JSONObject todoDeviceByXiaoMi(JSONObject jsonObject, String deviceSn) throws Exception {
        Integer gatewayType=redisDeviceService.getDeviceType(deviceSn);
        String deviceData=jsonObject.getString("deviceData");
        if(19==gatewayType){//温湿度传感器
            String [] datas=deviceData.split(",");
            Double t= Double.parseDouble( datas[0].split("=")[1])/100;
            if(t>28 || t<22){
                System.out.println(deviceSn+":温度异常:"+t);
                jsonObject.put("message",deviceSn+":温度异常:"+t);
                //TODO 调用业务接口
            }else{
                jsonObject.put("message",deviceSn+":温度正常:"+t);
            }
        }

        //烟感传感器
        if(18==gatewayType){
            String [] datas=deviceData.split("=");
            if("Yes".equals(datas[1])){
                System.out.println(deviceSn+"烟雾有报警:"+deviceData);
                jsonObject.put("message",deviceSn+":烟雾数据异常:"+deviceData);
                //TODO 调用业务接口
            }
        }
        return jsonObject;
    }
}
